<template>
  <page-header-wrapper :title="false">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="16">
            <a-col :span="6">
              <a-form-item label="订单号">
                <a-input v-model="queryParam.orderNo" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="手机号">
                <a-input v-model="queryParam.mobile" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :span="4" style="text-align: right;">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" style="margin-right: 8px" @click="handleSearch">查询</a-button>
                <a-button style="margin-right: 8px" @click="handleReset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table
        ref="table"
        size="default"
        :scroll="{x: true}"
        :columns="columns"
        :data="loadData"
        :pageNum="pageNum"
        :pageSize="pageSize"
        rowKey="id">
        <div slot="action" slot-scope="text, record">
          <a type="primary" v-if="record.payState === 1 && record.refundState === 0 && hasRefund"  @click="handleRefund(record)">退款</a>
          <span v-if="record.refundState === 1">已退款</span>
        </div>
        <div slot="refundState" slot-scope="text, record">
          <span v-if="record.refundState === 0">/</span>
          <span style="color: red;" v-if="record.refundState === 1">{{record.refundStateName}}</span>
        </div>
        <div slot="refundFee" slot-scope="text, record">
          <span v-if="record.refundState === 0">/</span>
          <span style="color: red;" v-if="record.refundState === 1">￥{{record.refundFee * 0.01}}</span>
        </div>
        <div slot="orderFee" slot-scope="text, record">
          <span>￥{{record.orderFee}}</span>
        </div>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import { listByPage, refundOrder } from '@/api/order'
import storage from 'store'
export default {
  name: 'TableList',
  components: { STable },
  data () {
    return {
      queryParam: {},
      pageNum: 1,
      pageSize: 10,
      visible: false,
      mdl: null,
      confirmLoading: false,
      columns: [
        {
          title: '订单号',
          width: 150,
          align: 'center',
          dataIndex: 'orderNo'
        },
        {
          title: '订单类型',
          width: 150,
          align: 'center',
          dataIndex: 'orderTypeName'
        },
        {
          title: '会员',
          width: 150,
          align: 'center',
          dataIndex: 'userIdName'
        },
        {
          title: '订单金额',
          width: 100,
          align: 'center',
          dataIndex: 'orderFee',
          scopedSlots: { customRender: 'orderFee' }
        },
        {
          title: '下单时间',
          width: 150,
          align: 'center',
          dataIndex: 'orderTime'
        },
        {
          title: '支付状态',
          width: 150,
          align: 'center',
          dataIndex: 'payStateName'
        },
        {
          title: '支付时间',
          width: 200,
          align: 'center',
          dataIndex: 'payTime'
        },
        {
          title: '退款状态',
          width: 150,
          align: 'center',
          dataIndex: 'refundStateName',
          scopedSlots: { customRender: 'refundState' }
        },
        {
          title: '退款金额',
          width: 150,
          align: 'center',
          dataIndex: 'refundFee',
          scopedSlots: { customRender: 'refundFee' }
        },
        {
          title: '退款时间',
          width: 200,
          align: 'center',
          dataIndex: 'refundTime'
        },
        {
          title: '操作',
          width: 180,
          fixed: 'right',
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      hasRefund: false
    }
  },
  created () {
    let permissions = storage.get("permissions")
    if(permissions && permissions.indexOf("pai_order_refund")>=0){
        this.hasRefund = true
    }
  },
  methods: {
    handleSearch (e) {
      this.$refs.table.refresh()
    },
    handleReset () {
      this.queryParam = {}
      this.$refs.table.refresh()
    },
    loadData (params) {
      return listByPage(Object.assign(params, this.queryParam)).then(res => {
        return res.data
      })
    },
    handleRefund (vo){
        this.$confirm(`您确定要退款吗？`, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          refundOrder(vo.id).then(res => {
            this.$message.success('退款成功！')
            this.$refs.table.refresh()
          }).catch(() => {})
        }).catch(() => {})
    }
  }
}
</script>
